package com.patrol.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patrol.pojo.TravelPath;
import com.patrol.service.TravelPathService;
import com.patrol.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 房梦豪
 * @version 1.0
 * @description: TODO
 * @date 2021/8/12 17:00
 */
@CrossOrigin
@Api(tags = "路径接口")
@RestController
@RequestMapping("/api/travelpath")
public class TravelPathController {

    @Resource
    private TravelPathService travelPathService;

    @ApiOperation(value = "根据id查询一条记录")
    @GetMapping("/getOneById/{id}")
    public Result<TravelPath> selectOneById(@PathVariable Integer id) {
        TravelPath travelPath = travelPathService.selectOneByTravel(id);
        return Result.ok(travelPath);
    }

    @ApiOperation(value = "根据ID更新一条记录")
    @PutMapping("/updateOneById")
    public Result updateOneById(TravelPath travelPath) {
        if (travelPath == null) {
            return Result.fail("更新失败，请检查数据是否为空");
        }
        boolean b = travelPathService.updateOneById(travelPath);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation(value = "根据ID删除一条记录")
    @DeleteMapping("/deleteOneById/{id}")
    public Result deleteOneById(@PathVariable Integer id) {
        boolean b = travelPathService.deleteOneById(id);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation(value = "添加一条记录")
    @PostMapping("/add")
    public Result insert(TravelPath travelPath) {
        boolean b = travelPathService.insertOne(travelPath);
        if (b) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/getAll/{page}/{limit}")
    public Result selectAll(@PathVariable("page") long page,
                            @PathVariable("limit") long limit) {
        Page<TravelPath> travelPathPage = travelPathService.selectAll(page, limit);
        return Result.ok(travelPathPage);
    }

    @ApiOperation(value = "根据用户ID查询该用户的所有点位")
    @GetMapping("/getLocation/{userId}")
    public Result selectUserAllLocation(@PathVariable Integer userId) {
        Map<Integer, List<Double>> map = travelPathService.selectUserAllLocation(userId);
        return Result.ok(map);
    }

    @ApiOperation(value = "根据用户ID和指定日期查询该用户的所有点位")
    @GetMapping("/getLocation/{userId}/{startDate}/{endDate}")
    public Result selectUserLocationByDate(@PathVariable("userId") Integer userId,
                                           @PathVariable("startDate") String startDate,
                                           @PathVariable("endDate") String endDate) {
        Map<Integer, List<Double>> map = travelPathService.selectUserLocationByDate(userId, startDate, endDate);
        return Result.ok(map);
    }
}
